#include "obstacles.h"

#include "physicsmanager.h"


Obstacles::Obstacles(Ogre::SceneManager *p_OgreSceneManager)
{
	m_OgreSceneManager = p_OgreSceneManager;

	SetupScene();
}


void Obstacles::SetupScene()
{
	// Create Object rootnode
	m_ObjectRootNode = m_OgreSceneManager->getRootSceneNode()->createChildSceneNode("ObstaclesRootNode");

    ObstacleLoader loader;
	   loader.parseDotScene("Obstacles.SCENE","General", m_OgreSceneManager, m_ObjectRootNode);

}

void Obstacles::GetExplosionParticle(Ogre::SceneNode* p_ObstacleNode)
{

    m_OgreSceneManager->destroyAllParticleSystems();
    m_ExplosionParticleSys = m_OgreSceneManager->createParticleSystem("ps", "mySplitterTemplate");


    Ogre::SceneNode* ParticleNode = p_ObstacleNode->createChildSceneNode();
    ParticleNode->attachObject(m_ExplosionParticleSys);

}

void Obstacles::Explode(Player* p_Player)
{


    // THIS IS FOR TESTING THE EXPLOSIONS
    // get Position of player
    Ogre::Vector3 PlayerPosition = p_Player->GetPosition();
    //std::cout << "PlayerPos " << PlayerPosition.x << " " << PlayerPosition.y << " " <<PlayerPosition.z << std::endl;

    Ogre::Real MinDistance = 2000;
    Ogre::SceneNode* NearestChild;
    // Iterate over Obstacles
    Ogre::SceneNode::ChildNodeIterator ChildNodeIt = m_ObjectRootNode->getChildIterator();

    while (ChildNodeIt.hasMoreElements())
    {
        // Get Obstacle Child nodes
        Ogre::SceneNode* Child = static_cast<Ogre::SceneNode*>(ChildNodeIt.getNext());

        // Distance between player and current obstacle
        Ogre::Real Distance = Child->getPosition().distance(PlayerPosition);
        // save min distance = nearest obstacle
        if (Distance < MinDistance)
        {
            MinDistance = Distance;
            NearestChild = Child;
        }

    }

    //std::cout << "| "<< NearestChild->getName() << " Position " << NearestChild->getPosition().x << " " << NearestChild->getPosition().y << " " <<NearestChild->getPosition().z << std::endl;
    //std::cout << "Distance " << MinDistance << std::endl;
    GetExplosionParticle(NearestChild);

    // get the entity/ies at the nearest childnode
//    Ogre::SceneNode::ObjectIterator ObjectIt = NearestChild->getAttachedObjectIterator();
//    while (ObjectIt.hasMoreElements())
//    {
//        Ogre::Entity* Entity = static_cast<Ogre::Entity*>(ObjectIt.getNext());
//        std::cout << "|- "<< Entity->getName() <<  std::endl;
//        Entity->setVisible(false);
//    }
}

Obstacles::~Obstacles()
{
    // Remove Particles
    m_OgreSceneManager->destroyAllParticleSystems();
    Ogre::ParticleSystemManager::getSingletonPtr()->removeAllTemplates();

    // Destroy all entities
    Ogre::SceneNode::ChildNodeIterator ChildNodeIt = m_ObjectRootNode->getChildIterator();

    while (ChildNodeIt.hasMoreElements())
    {
        // Get Obstacle Child nodes
        Ogre::SceneNode* Child = static_cast<Ogre::SceneNode*>(ChildNodeIt.getNext());


        // Destroy all entities
        Ogre::SceneNode::ObjectIterator EntIter = Child->getAttachedObjectIterator();

        while (EntIter.hasMoreElements())
        {
            // Get Obstacle Child nodes
            Ogre::Entity *Entity = static_cast<Ogre::Entity*>(EntIter.getNext());

            m_OgreSceneManager->destroyEntity(Entity);

        }

        Child->removeAndDestroyAllChildren();

    }

    m_ObjectRootNode->removeAndDestroyAllChildren();
    m_OgreSceneManager->getRootSceneNode()->removeAndDestroyChild(m_ObjectRootNode->getName());
    m_ExplosionParticleSys = 0;
}
